package com.madou.dbtool.redis.panel.ui.types;

import cn.hutool.json.JSONUtil;
import com.madou.common.annotation.HideClass;
import com.madou.dbtool.common.icon.AllIcons;
import com.madou.dbtool.common.messages.DBToolBundle;
import com.madou.dbtool.language.enums.ContentTypeEnum;
import com.madou.dbtool.redis.manager.RedisSourceManager;
import com.madou.dbtool.redis.utils.BytesToStringUtil;
import com.madou.dbtool.ui.editor.MAdjustmentListener;
import com.madou.dbtool.ui.editor.MEditor;
import com.madou.dbtool.ui.editor.MStyledEditorKit;
import redis.clients.jedis.Protocol;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * String类型的预览面板
 * @author miukoo
 */
@HideClass
public class StringTypePanel extends JPanel {

    String selectKey;
    RedisSourceManager redisSourceManager;
//    JTextPane editor = new JTextPane();
    MEditor editor;
    private JToolBar pageBar;
    private JButton applayButton;
    private JButton formatButton;
    int dbIndex;

    StringTypePanel(RedisSourceManager redisSourceManager,String selectKey,int dbIndex){
        this.selectKey = selectKey;
        this.redisSourceManager = redisSourceManager;
        this.setLayout(new BorderLayout());
        editor= new MEditor(ContentTypeEnum.JSON);
        editor.setEditorKit(new MStyledEditorKit(ContentTypeEnum.JSON));
        JScrollPane jScrollPane = new JScrollPane(editor);
        jScrollPane.getVerticalScrollBar().addAdjustmentListener(new MAdjustmentListener());
        this.add(jScrollPane, BorderLayout.CENTER);
        this.dbIndex = dbIndex;
//        editor.setContentType("text/json");
//        editor.setEditable(true);
//        editor.setPreferredSize(new Dimension(-1,-1));
        initData();
        initPageBar();
    }

    private void initPageBar() {
        applayButton= new JButton(DBToolBundle.message("redis.string.type.panel.button.save"),AllIcons.Table.APPLAY);
        formatButton= new JButton(DBToolBundle.message("redis.string.type.panel.button.format"),AllIcons.Common.FORMAT);
        pageBar = new JToolBar();
        pageBar.setOpaque(false);
        pageBar.setFloatable(false);
        pageBar.add(applayButton);
        pageBar.add(formatButton);
        this.add(pageBar,BorderLayout.NORTH);

        applayButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String text = editor.getText();
                byte[][] parm = new byte[2][1];
                parm[0]=BytesToStringUtil.string2Byte(selectKey);
                parm[1]=text.getBytes();
                redisSourceManager.execute(dbIndex, Protocol.Command.SET,parm);
            }
        });
        formatButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                editor.setText(JSONUtil.formatJsonStr(editor.getText()));
            }
        });
    }

    private void initData() {
        byte[] execute = (byte[]) redisSourceManager.execute(dbIndex,"GET " + selectKey);
        if(execute!=null){
            String text = new String(execute);
            try{
                text = JSONUtil.formatJsonStr(text);
            }catch (Exception e){
                e.printStackTrace();
            }
            editor.setText(text);
        }
    }

}
